$(document).ready(function(){

       //读取登录状态
       var if_login=$.cookie("status");
       //读取用户名
       var username=$.cookie("username");
       
       if(if_login==1){
           $("#login_region").text(username);
           $("#dropdown-login-item").css("display","none");
           $("#dropdown-exit-item").css("display","block");
           
       }else if(if_login==0){
           $("#login_region").text("登录/注册");
           $("#dropdown-login-item").css("display","block");
           $("#dropdown-exit-item").css("display","none");
       }


    //判断是租赁模式 先租后买还是以租代售  0先租后买  1以租代售
    var if_installment=Number(sessionStorage.if_installment);
    if(if_installment==0){
        //判断租赁类型   月租还是日租
        var if_month=sessionStorage.dayOrMonth;
        if(if_month==1){
            $("#pattern").text("先租后买");
            $("#rent_pattern").text("月租");
            $("#time-title").text("租赁时间：");
            $("#dayormonth").text("元/月");
            $("#rent-time-dayormon").text("月");
            $("#rent-time").text(sessionStorage.rentalDay);
            $("#rent-single-price").text(sessionStorage.monthprice);
        }else if(if_month==0){
            $("#pattern").text("先租后买");
            $("#rent_pattern").text("日租");
            $("#time-title").text("租赁时间：");
            $("#dayormonth").text("元/日");
            $("#rent-time-dayormon").text("日");
            $("#rent-time").text(sessionStorage.rentalDay);
            $("#rent-single-price").text(sessionStorage.dayprice);
        }
        else{
            $("#pattern").text("直接购买");
            $("#rent-single-title").text("商品单价：");
            $("#rent-single-price").text(Number(sessionStorage.rental).toFixed(2)+"元");
        }
        $("#rent-money").text(Number(sessionStorage.rental).toFixed(2));
        $("#subtotal").text(Number(sessionStorage.rental).toFixed(2));
        $("#actual-pay-money").text(Number(sessionStorage.rental).toFixed(2));
    }
    else if(if_installment==1){
        $("#pattern").text("以租代售");
        $("#time-title").text("分期期数：");
        $("#rent-time").text(sessionStorage.installment_month);
        $("#rent-single-title").text("每期应付：");
        $("#rent-single-price").text(sessionStorage.installment_price);
        $("#dayormonth").text("元/期");
        $("#rent-money").text(Number(sessionStorage.installment_price).toFixed(2));
        $("#subtotal").text(Number(sessionStorage.installment_price).toFixed(2));
        $("#actual-pay-money").text(Number(sessionStorage.installment_price).toFixed(2));
    }
    $("#seller-name").text(sessionStorage.seller);
    $("#product-name").text(sessionStorage.productname);
    $("#pro-img").attr("src",sessionStorage.productimg);    
    
   

    var data={
        name:username,
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/userad/getdata", 
        data:data,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
           
            for(var i=1;i<=data.data.length;i++){
                var h1='<div class="address-area"><div class="consignee-info">';
                var h2='<span>'+data.data[i-1].addressee+'</span>';
                var h3='</div><div class="address-info">'+'<span>'+data.data [i-1].address+'</span>';
                var h4='<span>'+data.data[i-1].phonenumber+'</span>';
                var h5='</div><i></i></div>';
                var content=h1+h2+h3+h4+h5;
                $(".address-list").append(content);
            }
        },
        error: function(xhr, type) {
            console.log("确认订单收获地址Error");
        }
     });
});


$("#inputsipnner-pro-num").on("change",function(event){
    var single_price=Number($("#rent-money").text())
    var rent_nums=Number( $(this).val());
    var newsubtotal=single_price*rent_nums;
    $('#subtotal').html(newsubtotal.toFixed(2));
    $('#actual-pay-money').html(newsubtotal.toFixed(2));

});

var final_adrr;
var final_consignee_name;
var final_phone_number;

//给地址加active并给提交订单区域收获信息传值
$(".address-list").on("click",".address-area", function(){
    $(".address-area").removeClass("active");
    $(this).addClass("active");
    final_consignee_name=this.children[0].firstChild.innerText;
    
    final_adrr=this.children[1].children[0].innerText;
    final_phone_number=this.children[1].children[1].innerText;

    $("#final-addr").text(final_adrr);
    $("#final-consignee").text(final_consignee_name);
    $("#final-phone").text(final_phone_number);


})



//提交订单按钮
$(".submit-btn").click(function(){

    //判断是租赁模式 先租后买还是以租代售  0先租后买  1以租代售
    var if_installment=Number(sessionStorage.if_installment);
    if(if_installment==0){
        var rental=Number( $('#actual-pay-money').text());
        var data={
            dayOrMonth:sessionStorage.dayOrMonth,
            num: Number( $("#inputsipnner-pro-num").val()),
            productId:sessionStorage.productId,
            purchaser:sessionStorage.purchaser,
            rental:rental,
            rentalDay:sessionStorage.rentalDay,
        //  seller:pro_detail.seller,
            status:sessionStorage.status,//是否直接买下
            address:final_adrr,
            phoneNumber:final_phone_number,
            receiver:final_consignee_name,
        }

        $.ajax({
            type: 'GET',
            url: "http://8.130.103.251:8080/order/addOrder1", 
            data: data,  
            ContentType: 'application/json;charset=utf-8',
            success: function(data) { 
                window.location.href=data;
            },
            error: function(xhr, type) {
                console.log("Error");
            }
        });
    }
    else if(if_installment==1){
        var price=Number( $('#actual-pay-money').text());
        var data={
            address:final_adrr,
            month:sessionStorage.installment_month,
            num:Number( $("#inputsipnner-pro-num").val()),
            phoneNumber:final_phone_number,
            price:price,
            productId:sessionStorage.productId,
            purchaser:sessionStorage.purchaser,
            receiver:final_consignee_name,
        }
        $.ajax({
            type: 'GET',
            url: "http://8.130.103.251:8080/order/fenqifukuan", 
            data: data,  
            ContentType: 'application/json;charset=utf-8',
            success: function(data) { 
                window.location.href=data;
            },
            error: function(xhr, type) {
                console.log("Error");
            }
        });
    }
});

//导航栏顶部跳转数码专区
$("#digital-area").click(function(){
    $.cookie('pro_type',"数码配件",{ expires: 7 });
    window.open("tags.html");
});

//退出按钮的点击事件
$("#dropdown-exit-item").on("click",function () { 
    $.cookie('username', null);
    $.cookie('status', '0', { expires: 7 });
    window.location.href = "index.html";
    
})